from easydict import EasyDict as edict
import os
import json
import logging

repo_config=edict()
repo_config.endpoint="10.1.9.54:9003"
repo_config.user="admin"
repo_config.pwd="123@abc.com"

def get_default_config(target="rknn"):
    basic_config = edict()
    basic_config.source_framework = "caffe"
    basic_config.mean = [0, 0, 0]
    basic_config.scale = [1, 1, 1]
    basic_config.work_dir = ""
    basic_config.model_name = ""
    basic_config.model_version = ""
    basic_config.load_after_convert = False
    basic_config.cache_repo = "./tmp/"
    basic_config.targe_framework = "rknn"

    # rknn
    if target=='rknn':
        rknn_config = basic_config
        rknn_config.reorder = [0, 1, 2]
        rknn_config.shape = []  # only mxnet need this config
        rknn_config.log_level = "error"
        rknn_config.targe_framework = "rknn"

        return rknn_config
    else:
        return basic_config


def generate_config(config_json,target="rknn"):
    if not os.path.exists(config_json):
        raise FileNotFoundError("{0} not found".format(config_json))
    
    with open(config_json,'r') as f:
        json_dict=json.load(f)

    config = get_default_config(target)
    for k,v in json_dict.items():
        if k in config:
            config[k]=v

    config.work_dir=os.path.dirname(config_json)
    model_version = os.path.dirname(os.path.abspath(config_json)).split("/")[-2]
    model_name= os.path.dirname(os.path.abspath(config_json)).split("/")[-3]
    config.model_version=model_version
    config.model_name=model_name
    return config
